
var vm = new Vue({
	el:'#rrapp',
	data:{
	    q:{
            searchName: null,
            sidx: null,
            order: null
        },
        page:{total:0,limit:30,page:1},
        dataList:[],
        isadd: false,
		showList: true,
		showView: false,
		title: null,
        addFlag: false,
        editFlag: false,
		sysLog: {},

        columnsList: [
          {name: '用户名', field: 'username' , isShow: true},
          {name: '用户操作', field: 'operation' , isShow: true},
          {name: 'IP地址', field: 'ip' , isShow: true},
          {name: '状态', field: 'stauts' , isShow: true},
          {name: '备注', field: 'remark' , isShow: true},
          {name: '时间', field: 'createDate' , isShow: true},
        ],
        stautsList: [
          {'value': '0', 'text': '成功'},
          {'value': '1', 'text': '失败'}
        ],
        stautsMap: {},
        stautsSearchList: {},
		rules: {
          id: [
            {required: true, message: '请输入', trigger: 'blur'},
            {min: 2, max: 20, message: '长度在 2 到 20 个字', trigger: 'blur'}
          ],
          test:[]
       }
	},
    mounted: function () {
        this.stautsMap = list2Map(this.stautsList)
        this.getDataList();
    },
	methods: {
		query: function () {
			vm.reload();
		},
        getDataList:function () {
		    debugger
            this.q.page = this.page.page;
            this.q.limit = this.page.limit;

            $.ajax({
                type: "POST",
                url: "../syslog/list",
                data: JSON.stringify(this.q),
                success: function (r) {
                    if (r.code == 0) {
                        //debugger;
                        vm.dataList = r.data.list;
                        vm.page.total = r.data.totalCount;
                    } else {
                        toastr.error(r.message);
                    }
                }
            });
        },
		add: function(){
		    vm.isadd = true; 
			vm.showList = false;
			vm.showView = false;
			vm.title = "新增";
			vm.sysLog = {};
		},
		update: function (id) {
			if(id == null){
				return ;
			}
			vm.showList = false;
			vm.showView = false;
            vm.title = "修改";
            vm.getInfo(id)
		},
		view: function(id){
        	vm.title = "查看";
        	vm.getInfo(id);
        	vm.showList = false;
        	vm.showView = true;
        },
        changeToUpdate:function(){
            vm.showView = false;
        },
		saveOrUpadateValid:function (valid){
            if (valid) {
                var url = vm.isadd ? "../syslog/save" : "../syslog/update";
                $.ajax({
				type: "POST",
			    url: url,
			    data: JSON.stringify(vm.sysLog),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.message);
					}
				}
			});
            }else{
                alert('验证未通过');
            }
        },
		saveOrUpdate: function (event) {
			this.$refs['sysLogForm'].validate(vm.saveOrUpadateValid);
		},
		del: function (ids) {

			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../syslog/delete",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
                            alert('删除成功', function(index){
                                vm.reload();
                            });
						}else{
							alert(r.message);
						}
					}
				});
			});
		},
		delOne: function (id) {

            var ids = [];
            ids[0] = id;

        	confirm('确定要删除该记录？', function(){
        		$.ajax({
        			type: "POST",
        			url: "../syslog/delete",
        			data: JSON.stringify(ids),
        			success: function(r){
                        if(r.code == 0){
                            alert('删除成功', function(index){
                                vm.reload();
                            });
                        }else{
                            alert(r.message);
                        }
        			}
        		});
        	});
        },
		getInfo: function(id){
			$.get("../syslog/info/"+id, function(r){
                vm.sysLog = r.data;
            });
		},
        handleCommand:function (commond) {
            if(commond=='d')
            {
                location.href = "../syslog/download?query=" + escape(JSON.stringify(vm.q));
                return;
            }

            if(vm.total>5000)
            {
                toastr.error("导出数据条数不能超过5000条,请用sql从数据库导出!");
                return;
            }

            if(commond=='a')
            {
                $("#data").tableExport({type:'csv'},'data');
                return;
            }

            if(commond=='b')
            {
                $("#data").tableExport({type:'txt'},'data');
                return;
            }

            if(commond=='c')
            {
                $("#data").tableExport({type:'excel',escape:false,
                    mso: {fileFormat:'xlsx',
                        excelstyles: ['border-bottom', 'border-top', 'border-left', 'border-right'],
                        worksheetName: ['Data']}},'data');
                return;
            }
        },
        exportExl: function () {
            if(vm.total>5000)
            {
                toastr.error("导出数据条数不能超过5000条,请用sql从数据库导出!");
                return;
            }

            if(vm.total>5000)
            {
                toastr.error("导出数据条数不能超过5000条,请用sql从数据库导出!");
                return;
            }

            $("#data").tableExport({type:'excel',escape:false,
                mso: {fileFormat:'xlsx',
                    excelstyles: ['border-bottom', 'border-top', 'border-left', 'border-right'],
                    worksheetName: ['Data']}},'data');

        },

        pageSizeChange:function (pageSize) {
            this.page.limit = pageSize;
            this.reload();
        },
        pageCurrentChange:function (page) {
            this.page.page = page;
            this.reload();
        },
        pageChange:function () {
            this.reload();
        },
		reload: function (event) {
			this.isadd = false;
			this.showList = true;
            this.getDataList();
		}
	}
});
